//
//  QpFoodDetailsViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/10.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpFoodDetailsViewController.h"

#import "QpFoodDetailsViewController+shouCang.h"
@interface QpFoodDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)NSMutableArray *dataArr;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *headerArr;/**<tableView头部数组*/

@property(nonatomic, strong)NSArray *arr;


@end

@implementation QpFoodDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    


    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url:  %@",  [self.manager applicationDocumentsDirectory]);

    //数据处理
    [self dataHandler];
    
    /**<创建tableView*/
    [self creatTableView];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"shoucang"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(shoucang)];
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shoucang
{
    
        

    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"title", @"%@" ], self.name];
    
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"ShouCang"inManagedObjectContext:self.manager.managedObjectContext];
    
   self.arr = [self selectHandler:predicate];
   
    
    
    if (_arr.count == 0 ) {
        

        [self addHandler:entity name:_name cityid:_cityid photo:_photo];

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
       
        
    
    }
    
    else{
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已经收藏过了" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
       
    
    
    
}

//数据处理
- (void)dataHandler
{
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@",self.cityid];
    NSString *str = @"&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
    
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"%@%@",url,str] completion:^(id result) {
       
        
      

        
        /**<设置头部内容*/
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 90)];
        //设置图片
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 360 /710)];
        //获取图片
        [img sd_setImageWithURL:[NSURL URLWithString:result[@"data"][@"photo"]]];
        
               [headerView addSubview:img];
        //设置标题
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(30, img.frame.size.height + 30, SCREEN_WIDTH - 60, 70)];
        
        title.font = [UIFont systemFontOfSize:25];
        title.numberOfLines = 2;
        title.textAlignment = NSTextAlignmentCenter;
        //赋值
        title.text = result[@"data"][@"title"];
        
        [headerView addSubview:title];
        //设置正文
        UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(10, img.frame.size.height + 100, SCREEN_WIDTH - 20, 140)];
        text.numberOfLines = 0;
        text.font = [UIFont systemFontOfSize:15];
        //赋值
        text.text = result[@"data"][@"description"];
        [headerView addSubview:text];
        
        self.dataArr = [QpFootDetailModel modelHanderWithArray:result[@"data"][@"pois"]];
        [_tableView reloadData];
    
    }];
    
}

- (void)creatTableView
{
   //创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //设置代理人
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //注册重用池
    
    [_tableView registerClass:[QpFootDetailsTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
}

#pragma mark - 协议必须实现的两种方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QpFootDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    QpFootDetailModel *qp = _dataArr[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:qp.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    cell.firstname.text = qp.firstname;
    cell.desc.text = qp.desc;
   
    
    
    return cell;

    
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消tableview 选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 跳转隐藏 tabbar
    self.hidesBottomBarWhenPushed=YES;
    QpFoodDetailsSecondViewController *qp = [[QpFoodDetailsSecondViewController alloc]init];
   QpCityDetailModel *q = _dataArr[indexPath.row];
    qp.cellid = q.cellid;
    [self.navigationController pushViewController:qp animated:YES];

}
#pragma mark -  设置行高
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QpFootDetailModel *qp = _dataArr[indexPath.row];
    // 得到label高度
    CGFloat h = [QpFootDetailsTableViewCell heightWithText:qp.desc];
    return h + (SCREEN_WIDTH - 30)*360 / 710 + 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
